package com.genshang.restaurant.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.genshang.restaurant.entity.Accumulatelisttable;
import com.genshang.restaurant.entity.Consumptionlist;
import com.genshang.restaurant.entity.Customerlist;
import com.genshang.restaurant.mapper.ConsumptionlistMapper;
import com.genshang.restaurant.result.ResultBody;
import com.genshang.restaurant.service.IAccumulatelisttableService;
import com.genshang.restaurant.service.IConsumptionlistService;
import com.genshang.restaurant.service.ICustomerlistService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类 用户消费记录表
 * </p>
 *
 * @author Alex
 * @since 2020-09-26
 */
@Service
public class ConsumptionlistServiceImpl extends ServiceImpl<ConsumptionlistMapper, Consumptionlist> implements IConsumptionlistService {

    @Autowired
    private IAccumulatelisttableService accumulatelisttableService;

    @Autowired
    private ICustomerlistService customerlistService;

    @Override
    public ResultBody consume(String nickname, String shopId, String consumerId, String finalPrice) {
        Consumptionlist consumptionlist = new Consumptionlist();
        consumptionlist.setTime(System.currentTimeMillis());
        consumptionlist.setShopuid(shopId);
        double rate = 20;
        if ("ababaaaabH1: com.restaurant.application.shop.Shop".equals(shopId) || "01".equals(shopId)) {
            consumptionlist.setShopname("艮上·环宇荟店");
        } else {
            consumptionlist.setShopname("艮上·铜仁路店");
            rate = 10;
        }
        Customerlist customerlist = customerlistService.getCustomerlistById(consumerId);
        consumptionlist.setCustomerid(consumerId);
        consumptionlist.setCustomername(customerlist.getNickname());
        Double price = Double.valueOf(finalPrice);
        consumptionlist.setTotalprice(price);
        consumptionlist.setFinalprice(price);
        Long accumulatepoints = (long) Math.floor(price / rate);
        consumptionlist.setAccumulatepoints(accumulatepoints);
        this.save(consumptionlist);

        Accumulatelisttable accumulatelisttable = new Accumulatelisttable();
        accumulatelisttable.setCustomername(customerlist.getNickname());
        accumulatelisttable.setCreatetime(System.currentTimeMillis());
        accumulatelisttable.setPoints(accumulatepoints);
        accumulatelisttable.setType("消费增加");
        accumulatelisttable.setAction("消费增加");
        accumulatelisttable.setCustomerid(consumerId);
        Long currentPoint = 0L;
        Accumulatelisttable currentAccumulatelisttable = accumulatelisttableService.getCurrentByCustomerId(consumerId);
        if (null != currentAccumulatelisttable) {
            currentPoint = currentAccumulatelisttable.getCurrentpoint();
        }
        accumulatelisttable.setCurrentpoint(currentPoint + accumulatepoints);
        accumulatelisttable.setPhone(customerlist.getPhone());
        accumulatelisttableService.save(accumulatelisttable);
        return ResultBody.success("保存成功");
    }

    @Override
    public ResultBody getList(Long pageSize, Long currentPage, String shopId, String consumerId, String minPrice, String maxPrice) {
        QueryWrapper<Consumptionlist> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("SHOPUID", shopId);
        queryWrapper.eq("CUSTOMERUID", consumerId);
        if (StringUtils.isNotBlank(minPrice)) {
            queryWrapper.gt("FINALPRICE", minPrice);
        }
        if (StringUtils.isNotBlank(maxPrice)) {
            queryWrapper.lt("FINALPRICE", maxPrice);
        }
        Page page = new Page(currentPage, pageSize);
        IPage<Consumptionlist> iPage = this.page(page, queryWrapper);
        return ResultBody.success(iPage.getRecords());
    }
}
